/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////Author: Shahab.Alishahian@gmail.com SA	
///////////////Target:The following methods are here to handle all GAPI functions.
///////////////Version:1.1
///////////////CopyRight: 2014 Noranj Co.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//*************************************************************************************************************
//******Facebook SDK setting

var appId='' ;
var accessToken='' ;
var appAccessToken='' ;
var channelUrl='' ;

if(window.location.href.indexOf("appspot") > -1){
	//JSITE
	appId ='538796809531275' ;
	channelUrl ='localhost' ;
	appAccessToken = '538796809531275|rXh4hMppl93RPlMc_2iITeRLHjs' ;
}
else{
	 //localhost
	appId='398847723578997' ;
	channelUrl='localhost' ;
	appAccessToken = '398847723578997|LicynEmM0LUt4dYaqH4ojjPYcBI' ;
}

$(document).ready(function() {
	//alert(document.getElementById('main')); //.click();
	$("#main homectrl").click();

	/*if($(".controlPlace").height()>0)
		$(".fancybox").height($(".controlPlace").height()+445);
	else
		$(".fancybox").height(500);
	*/
	 var myVar = setTimeout(function(){
		 console.log('timer test');
	 },1000);

	 window.fbAsyncInit = function () {
	
     FB.init({
         appId: appId, // App ID
         channelUrl: channelUrl, // Channel File
         status: true, // check login status
         cookie: true, // enable cookies to allow the server to access the session
         xfbml: true, // parse XFBML
         frictionlessRequests : true
     });
 };
 
 // Load the SDK Asynchronously
 (function (d) {
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {
         return;
     }
     js = d.createElement('script');
     js.id = id;
     js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
 } (document));


 function loggedin() {
	    FB.getLoginStatus(function (response) {
	    	//alert(response.status );
	    	loadparts(2);
	        if (response.status === 'connected') {
	            FB.api('/me', function (user) {
	                if (!($("#fbid").val()) && ($("#UserName").val())) {
	                    alert('Authentication Failed!');
	                }
	                else {
	                	//user.id = 824750206; //TODO:SA:Must be removed after testing.
	                    $("#fbid").val(user.id);
	                    $("#firstName").val(user.first_name);
	                    $("#lastName").val(user.last_name);
	                    $("#userName").val(user.email);
	                    $("#emailAddress").val(user.email);
	                    $("#userImage").attr("src",'https://graph.facebook.com/' + user.id + '/picture?width=155&Height=170');
	                    addUser(user);
	                }
	            });
	        }
	    });
	}
 

 
//************************************************************************************************************
	 function addUser(user){
			
			var tempdata = {};
			tempdata['address'] = '0';
			tempdata['age'] = '0';
			tempdata['city'] = '0';
			tempdata['country'] = '0';
			tempdata['dateCreated'] = returnToday();
			tempdata['email'] = user.email;
			tempdata['facebookId'] = user.id;
			tempdata['firstName'] = user.first_name;
			tempdata['gender'] = '0';
			tempdata['lastName'] = user.last_name;
			tempdata['merchantId'] = $("#jbMerchantId").val();
			setAddUser(tempdata);
		}

	 
//*************************************************************************************************************

//*************************************************************************************************************
function sendMessage(to,text) {
    FB.getLoginStatus(function (response) {
        FB.ui({
                 method: "send",
                 app_id:appId,
                 to:to,
                 picture: $('#jsSelectedImageURL').val(),
                 link: $('#jsSelectedBoardURL').val(),
                 description:text,
                 name: text,
                 display: 'popup'
             },
             function (param) {
                 console.log('done');
            });
         });
  }
//*************************************************************************************************************

function doSomething(){
	alert('testdynamic');
}


});


function uniqueId() {
	 var d = new Date();
	 var n = d.getTime();
	 return n; 
}

     